package com.ytd.common.dialog;

import android.content.Context;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;

import com.ytd.common.R;

public class ResultDialog extends HideNavigationDialog {
    int timeout = 0;
    Context mContext;
    private TextView waitText;

    private ImageView ivShow;

    private String text;
    private Runnable dismissRunnable = new Runnable() {
        @Override
        public void run() {
            dismiss();
        }
    };

    public ResultDialog(Context context, String result, int ivRes, int timeout, boolean isDismiss) {
        super(context);//设置样式
        mContext = context;
        getWindow().setGravity(Gravity.CENTER);
        setContentView(R.layout.dialog_result);
        waitText = (TextView) findViewById(R.id.dialog_txt);
        ivShow = (ImageView) findViewById(R.id.iv_show);
        waitText.setText(result);
        text = result;
        if (ivRes == 0) {
            ivShow.setImageResource(com.ytd.common.R.drawable.gif_progress);
        } else {
            ivShow.setImageResource(ivRes);
        }
        if (isDismiss) {
            this.setCanceledOnTouchOutside(false);
            //设置点击返回键不消失
            this.setCancelable(false);
        }
        this.timeout = timeout;
        if (timeout > 0) {
            ivShow.postDelayed(dismissRunnable, timeout * 1000);
        }
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        waitText.setText(text);
    }

    public void setIvRes(int res) {
        if (res == 0) {
            ivShow.setImageResource(R.drawable.gif_progress);
        } else {
            ivShow.setImageResource(res);
        }
    }

    public void resetTime() {
        ivShow.removeCallbacks(dismissRunnable);
        if (timeout > 0) {
            ivShow.postDelayed(dismissRunnable, timeout * 1000);
        }
    }

}
